<template>
  <div class="list-table">
    <div class="nospecialModel ">
      <div class="area-header">
        <span class="area-header-title">{{ $t('负面新闻') }}</span>
      </div>
      <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('新闻标题')" prop="newTitle">
          <el-input
            v-model="queryParams.newTitle"
            :placeholder="$t('新闻标题')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item :label="$t('新闻来源')" prop="newSource">
          <el-input
            v-model="queryParams.newSource"
            :placeholder="$t('新闻来源')"
            clearable
            @keyup.enter.native="queryHandle"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            v-hasPermi="['company:news:edit']"
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="addHandle"
          >{{ $t("新增") }}</el-button>
        </el-col>
      </el-row>
    </div>
    <div class="specialModel">
      <!-- 不可删除 -->
      <div class="area-header left">
        <span class="area-header-title">{{ $t('新闻信息') }}</span>
      </div>
      <!-- <el-form ref="queryForm" :model="queryParams" :inline="true">
        <el-form-item :label="$t('新闻分类')" prop="newTitle">
          <el-select v-model="queryParams.emotion" placeholder="请选择" clearable @change="getList">
            <el-option
              v-for="item in emotionOption"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue"
            />
          </el-select>
        </el-form-item>
      </el-form> -->
      <div class="right">
        <el-button
          v-hasPermi="['company:news:edit']"
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </div>
    </div>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="companyNewsList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('新闻标题')" align="center" prop="newTitle" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link
            class="link_a"
            type="primary"
            @click="openNews(scope.row)"
          >{{ scope.row.newTitle }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('新闻来源')" align="center" prop="newSource" show-overflow-tooltip width="120" />
      <el-table-column :label="$t('新闻时间')" align="center" prop="newTime" width="110">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.newTime) }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column :label="$t('标签')" align="center" prop="tags" show-overflow-tooltip /> -->
      <el-table-column :label="$t('新闻分类')" align="center" prop="emotion" show-overflow-tooltip width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.emotion=='-1'" style="color: #FF5722">负面</span>
          <span v-else-if="scope.row.emotion=='1'" style="color: #009688">正面</span>
          <span v-else-if="scope.row.emotion=='0'" style="color: #31BDEC">中性</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('新闻简介')" align="center" prop="abstracts" show-overflow-tooltip>
        <template slot-scope="scope">
          <span v-for=" (item,index) in scope.row.companyNewsTagsList" :key="index">
            <el-tag
              class="rim"
              size="mini"
              effect="plain"
              type="primary"
            >{{ item.tags }}</el-tag>
          </span>
          <p class="pre" type="text">{{ scope.row.abstracts }}</p>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="140">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['company:news:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['company:news:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新闻信息对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <companyNewsForm ref="companyNewsForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listCompanyNews, delCompanyNews, exportCompanyNews } from '@/api/company/news/companyNews'
import companyNewsForm from '@/views/company/news/companyNewsForm'
export default {
    components: {
        companyNewsForm
    },
  props: {
    companyId: {
      type: String,
      default: undefined
    }
  },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 新闻信息表格数据
          companyNewsList: [],
          // 弹出层标题
          title: '新闻信息',
          // 是否显示弹出层
          formOpen: false,
          // 查询参数
          queryParams: {
              companyId: undefined,
              newTitle: undefined,
              emotion: undefined,
              newSource: undefined,
              newTime: undefined,
              // emotion: '-1',
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
          },
          color: ['', 'success', 'info', 'warning', 'danger'],
          emotionOption: []
      }
  },
  created() {
       this.getDicts('emotion').then((response) => {
          this.emotionOption = response.data
        })
      this.getList()
  },
  methods: {
    /** 查询新闻信息列表 */
    getList() {
        this.loading = true
        this.queryParams.companyId = this.companyId
        this.queryParams.emotion = -1
        listCompanyNews(this.queryParams).then(response => {
            this.companyNewsList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            companyId: undefined,
            newTitle: undefined,
            newUrl: undefined,
            newSource: undefined,
            newTime: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            remark: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.companyNewsForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.companyNewsForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delCompanyNews(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        var queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return exportCompanyNews(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(function() {
        })
    },
    openNews(row) {
      if (row.newUrl.indexOf('http://') !== -1) {
        window.open(row.newUrl)
      } else if (row.newUrl.indexOf('https://') !== -1) {
        window.open(row.newUrl)
      } else {
        window.open('http://' + row.newUrl)
      }
    },
    classify(data) {
      if (data) {
        if (data === 1) {
            return '正面'
        } else if (data === -1) {
            return '负面'
        } else {
           return '中性'
        }
      } else {
        return ''
      }
    },
    randumColor() {
        const a = Math.random()
        const b = a * 5
        // 向下取整
        const c = Math.floor(b)// 向下取整
        return this.color[c]
    }
  }
}
</script>
<style scoped>
 .pre {
    white-space: pre-line !important;
    word-break: break-all;
    word-wrap: break-word;
    text-align:left;
    line-height: 18px;

}
.rim{
  margin: 0px 4px;
}
</style>
